package cn.xueden.common.result;

import lombok.Data;

import java.io.Serializable;

/**功能描述: 统一返回结果类
 * @author:梁志杰
 * @date:2025/6/8
 * @description:cn.xueden.common.result
 * @version:1.0
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回消息
     */
    private String msg;

    /**
      * 数据
     */
    private T data;

    /**
     * 成功返回，无参
     */
    public static <T> Result<T> success(){
        return success("返回成功");
    }
    /**
     * 成功返回 带消息参数
     */
    public static <T> Result<T> success(String msg){
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }

    /**
     * 带数据返回
     */
    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setMsg("返回成功");
        result.setData(data);
        return result;
    }
    /**
     * 带数据返回 带消息参数
     */
    public static <T> Result<T> success(String msg,T data){
        Result<T> result = new Result<>();
        result.setCode(1);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 带数据返回 带消息参数 带状态码
     */
    public static <T> Result<T> success(Integer code,String msg,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * 失败返回，无参
     */
    public static <T> Result<T> failed(){
        return result(0,"操作失败",null);
    }

    /**
     * 失败返回 带消息参数
     */
    public static <T> Result<T> failed(String msg){
        return result(0,msg,null);
    }
    /**
     * 带数据返回 带消息参数
     */
    public static <T> Result<T> failed(String msg,T data){
        return result(0,msg,data);
    }

    public static <T> Result<T> result(Integer code,String msg,T data){
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
}
